﻿using System;
using System.Text;
using NUnit.Framework;

namespace Testing.Helpers.Assertion
{
    /// <summary>
    /// Verify.
    /// </summary>
    public static class Verify
    {
        /// <summary> Errors. </summary>
        private readonly static StringBuilder _verificationErrors = new StringBuilder();

        /// <summary> Returns the sign there are any validation errors at the moment. </summary>
        public static bool IsContainErrors { get { return _verificationErrors.Length > 0; } }

        /// <summary>
        /// Clean error list.
        /// </summary>
        public static void CleanUp()
        {
            _verificationErrors.Clear();
        }

        /// <summary>
        /// Add error to list.
        /// </summary>
        /// <param name="action">Assert method.</param>
        public static void LogIfError(Action action)
        {
            try
            {
                action();
            }
            catch (AssertionException e)
            {
                _verificationErrors.AppendLine(e.Message);
            }
        }


        /// <summary>
        /// Assert results.
        /// </summary>
        public static void AssertResults()
        {
            Assert.IsTrue(_verificationErrors.Length == 0,
                "There are validation errors after test execution:\r\n {0}", _verificationErrors);
        }

        /// <summary>
        /// Assert results.
        /// </summary>
        /// <param name="titleMessage">Title.</param>
        public static void AssertResults(string titleMessage)
        {
            Assert.IsTrue(_verificationErrors.Length == 0,
                "There are validation errors after test execution:\r\n {0}\r\n{1}", titleMessage, _verificationErrors);
        }
    }
}
